﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;

namespace QLSTK_DAL_WS
{
    public class STKDAO : DataProvider
    {
        public STKDAO()
        {
            connect();
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            STKDTO STK = new STKDTO();
            STK.Maso = dt.Rows[i]["MaSo"].ToString();
            STK.MaloaiTK = dt.Rows[i]["MaLoaiTietKiem"].ToString();
            STK.Khachhang = dt.Rows[i]["KhachHang"].ToString();
            STK.Cmnd = dt.Rows[i]["CMND"].ToString();
            STK.Diachi = dt.Rows[i]["DiaChi"].ToString();
            STK.Ngaymo = DateTime.Parse(dt.Rows[i]["NgayMoSo"].ToString());
            STK.Sotiengoi = dt.Rows[i]["SoTienGoi"].ToString();
            STK.Ngayhethan = dt.Rows[i]["NgayHetHan"].ToString();
            STK.Solandaohan = int.Parse(dt.Rows[i]["SoLanDaoHan"].ToString());
            STK.LaiSuat = dt.Rows[i]["LaiSuat"].ToString();
            STK.Tinhtrang = bool.Parse(dt.Rows[i]["TinhTrang"].ToString());
            return (object)STK;
        }

        public STKDTO[] getSTK()
        {
            string str = "SELECT * FROM SOTIETKIEM";
            adapter = new OleDbDataAdapter(str, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            STKDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new STKDTO[n];
            for (i = 0; i < n; i++)
            {
                object STK = GetDataFromDataRow(dt, i);
                arr[i] = (STKDTO)STK;
            }
            return arr;
        }

        public void insert(STKDTO info)
        {
            String them = "insert into SOTIETKIEM('" + info.MaloaiTK + "','" + info.Khachhang + "','" + info.Cmnd + "','" + info.Diachi + "','" + info.Ngaymo.ToShortDateString() + "','" + info.Sotiengoi + "','" + info.Ngayhethan + "','" + info.Solandaohan + "','" + info.LaiSuat + "','" + info.Tinhtrang + "')";
            ExcuteNonQuery(them);
        }

        

    }
}